﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FSMSystem
{
    //保存所有的状态
    private List<FSMState> stateList;
    public FSMSystem()
    {
        stateList = new List<FSMState>();
    }
    //当前状态
    private FSMState currentState;
    public FSMState CurrentState { get { return currentState; } }

    //添加状态
    public void AddState(FSMState state)
    {
        //检查名字是否符合规范
        if (state == null)
            return;
        //如果是第一个状态 就设置为默认状态
        if (stateList.Count == 0)
        {
            stateList.Add(state);
            currentState = state;
            return;
        }
        //检查列表中是否已经包含了该状态
        if (!stateList.Contains(state))
            stateList.Add(state);
    }
    //删除状态
    public void DelateState(string stateName)
    {
        if (stateName == "")
            return;
        foreach (FSMState state in stateList)//遍历删除对应的状态
        {
            if (state.Name == stateName)
            {
                stateList.Remove(state);
                break;
            }
        }
        
    }

    //当前状态转换到另一个状态
    public void DoTransitionState(Transition transiton)
    {
        //检查状态是否符合规则
        if (transiton == Transition.NULL)
            return;
        //得到转换到的下一个状态的名字
        string NextStateName = currentState.GetOutState(transiton);
        if (NextStateName == "")
            return;
        //遍历转换到下一个状态
        foreach (FSMState state in stateList)
        {
            if (state.Name == NextStateName)
            {
                currentState.DoBeforeEnter();//转换之前做某些事
                currentState = state;
                currentState.DoAfterEnter();//转换后做某些事
                break; 
            }
        }
    }
}
